<?
    class TypesController extends Principio_Library_ControllerAction
    {
        public function init()
        {
            parent::init();
            $this->view->menu = array(
                                "Edit" => "edit",
                                "Customize Profile" => "customize",
                                "Delete" => "delete"
            );
        }


        public function indexAction()
        {
            $this->view->title = "SPA types";
        }
               
        
        private function validate($is_edit = false)
        {
            if ($this->checkPostItemString("type", "Type"))
                $this->view->model_type->setType($this->post_data['type']);

            if ($this->checkPostItemString("caption", "Caption"))
                $this->view->model_type->setCaption($this->post_data['caption']);

            if ($this->checkPostItem("firmware_id", "Firmware ID"))
                $this->view->model_type->setModelFirmware(Principio_Model_FirmwareMapper::get(intval($this->post_data['firmware_id'])));

            if (!$is_edit)
            {
                if ($this->checkPostItemInt("exts", "Ext count"))
                    $this->view->model_type->setExts($this->post_data['exts']);
            };

            if (!Principio_Library_Common::$is_error)
            {
                Principio_Library_Common::checkValidity(Principio_Model_TypeMapper::checkUniqueType($this->view->model_type), "SPA Type with type '".$this->view->model_type->getType()."' already exists");
                Principio_Library_Common::checkValidity(Principio_Model_TypeMapper::checkUniqueCaption($this->view->model_type), "SPA Type with caption '".$this->view->model_type->getCaption()."' already exists");
            };

            return !Principio_Library_Common::$is_error;
        }


        public function shortAction()
        {
            if (($this->is_post) && ($this->checkPostItemInt("id", "Type ID")) && ($this->checkPostItemInt("company_id", "Company ID")))
            {
                $this->view->model_type = Principio_Model_TypeMapper::get($this->post_data['id']);
                $this->view->model_company = Principio_Model_CompanyMapper::get($this->post_data['company_id']);
                $this->view->model_company_type_ext_template = Principio_Model_CompanyTypeExtTemplateMapper::get($this->view->model_company->getID(), $this->view->model_type->getID());

                if ($this->is_apply)
                {
                    for ($i = 0; $i <= $this->view->model_type->getExts(); $i++)
                    {
                        if (!isset($this->post_data["ext".$i."_template"]))
                            $this->view->model_company_type_ext_template->setTemplate($i, "");
                        else
                            $this->view->model_company_type_ext_template->setTemplate($i, $this->post_data["ext".$i."_template"]);
                    };

                    if (Principio_Model_CompanyTypeExtTemplateMapper::update($this->view->model_company_type_ext_template))
                        Principio_Library_Common::$msg = "Templates were updated.";
                };

                $this->view->title = "Overview of type [".$this->view->model_type->getCaption()."] in ".($this->view->model_company->isPool() ? "pool" : "company")." [".$this->view->model_company->getCaption()."]";
            };
        }
	
	
        public function editAction()
        {
            if (($this->is_post) && ($this->checkPostItemInt("id", "Type ID")))
            {
                $this->view->firmwares = Principio_Model_FirmwareMapper::get();
                $this->view->is_add = false;
                $this->view->action = "edit";
                $this->view->model_type = Principio_Model_TypeMapper::get($this->post_data['id']);
                $model_type_old = Principio_Model_TypeMapper::get($this->post_data['id']);

                if (($this->is_apply) && ($this->validate(true)))
                {
                    if (Principio_Model_TypeMapper::save($this->view->model_type))
                        Principio_Library_Common::$msg = "Type was updated.";
                };

                $this->view->title = "View and Edit type [".(Principio_Library_Common::$is_error ? $model_type_old->getCaption() : $this->view->model_type->getCaption())."]";
            };
        }
    

	
        public function customizeAction()
        {
            if (($this->is_post) && ($this->checkPostItemInt("id", "Type ID")))
            {
                $model_type = Principio_Model_TypeMapper::get($this->post_data["id"]);
                $this->view->action = "customize";
                $this->view->model_type_ext_template = Principio_Model_TypeExtTemplateMapper::get($this->post_data["id"]);

                if ($this->is_apply)
                {
                    for ($i = 0; $i <= $model_type->getExts(); $i++)
                    {
                        if (!isset($this->post_data["ext".$i."_template"]))
                            $this->view->model_type_ext_template->setTemplate($i, "");
                        else
                            $this->view->model_type_ext_template->setTemplate($i, $this->post_data["ext".$i."_template"]);
                    };

                    if (Principio_Model_TypeExtTemplateMapper::update($this->view->model_type_ext_template))
                        Principio_Library_Common::$msg = "Type templates were updated.";
                };

            };
        }

	
        public function addAction()
	    {
            if ($this->is_post)
            {
                $this->view->title = "Add new type";
		        $this->view->action = "add";
                $this->view->is_add = true;
                $this->view->model_type = new Principio_Model_Type();
                $this->view->firmwares = Principio_Model_FirmwareMapper::get();
                
                if ($this->is_apply)
		        {
                    if ($this->validate())
                    {
                        if (($type_id = Principio_Model_TypeMapper::save($this->view->model_type)) !== false)
                        {
                            $this->view->model_type->setID($type_id);
                            Principio_Library_Common::$msg = "Type was successfully added";
                            $this->view->is_add = false;
                            $this->view->action = "edit";
                            $this->view->title = "View and Edit type [".$this->view->model_type->getCaption()."]";
                        };
                    };
                };
            };
	    }


        public function deleteAction()
        {
            if ($this->is_post)
            {
                if ($this->checkPostItemInt("id", "Type ID"))
                {
                    if (Principio_Model_TypeMapper::delete($this->post_data["id"]))
                    {
                        Principio_Library_DHCP::generate();
                        Principio_Library_Common::$msg = "Type was deleted.";
                    };
                };
            };
        }
}